 <template>
  <div>
    <header>
      <span>搜索</span>
    </header>
    <main>
      <div class="sr">
        <input type="text" v-model="val" />
        <span @click="tj()">提交</span>
      </div>
      <ul>
        <li v-for="item in arr" :key="item">{{ item }}</li>
      </ul>
      <ul v-if="show">
        <li v-for="(i, index) in s" :key="i">
          {{ i }}<span @click="del(index)">X</span>
        </li>
      </ul>
       <button @click="delall()">删除全部</button>
    </main>
  </div>
</template>
 <script>
export default {
  data() {
    return {
      val: "",
      arrnew: [],
      arr: "",
      t: [],
      h: "",
      show: false,
      s: "",
    };
  },
  methods: {
    tj() {
      if (!this.val == "") {
        this.arrnew.push(this.val);
      }
      localStorage.val = JSON.stringify(this.arrnew);
      this.arr = JSON.parse(localStorage.val);
      this.arr = Array.from(new Set(this.arr));
      this.val = "";
      this.h = JSON.parse(localStorage.val);
      this.h = Array.from(new Set(this.h));
      this.show = false;
    },
    del(i) {
      this.s.splice(i, 1);
    //   this.s=JSON.parse(localStorage.val);
        localStorage.removeItem("val")
    },
    delall(){
    localStorage.clear()
    this.s=""
    }
  },
  computed: {},
  mounted() {
    if (localStorage.val) {
      this.show = true;
      this.h = JSON.parse(localStorage.val);
      this.h = Array.from(new Set(this.h));
      this.s = this.h.slice();
    }
  },
};
</script>
 <style lang='css'>
* {
  margin: 0;
  padding: 0;
}
header {
  width: 100%;
  height: 3rem;
  background: #3090e8;
  box-sizing: border-box;
  display: flex;
  justify-content: center;
  align-items: center;
}
header span {
  color: #ffff;
}
main {
  flex: 1;
  padding-top: 1rem;
}
.sr {
  width: 100%;
  height: 2.5rem;
  display: flex;
  justify-content: space-between;
  padding: 0rem 1rem;
  box-sizing: border-box;
  align-items: center;
}
.sr input {
  width: 17rem;
  height: 2rem;
  border: 1px solid #000;
  outline: none;
  background: #f2f2f2;
}
.sr span {
  width: 4rem;
  height: 2rem;
  background: #3090e8;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #ffffff;
  border-radius: 0.5rem;
}
ul {
  padding: 0rem 2rem;
  box-sizing: border-box;
}
ul li {
  list-style: none;
  width: 100%;
  height: 1rem;
  padding-bottom: 1rem;
  display: flex;
  justify-content: space-between;
}
button{
    position: fixed;
    top: 20rem;
    margin-left: 1rem;
}
</style>